/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetluc;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author Lone_Wolf
 */
public class ModifierEmploye extends JPanel {

    private Fenetre fenetre;
    private SearchResult result;
    private Employe employe;
    Font f = new Font("Times New Roman", Font.BOLD, 24);
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
    private JLabel lblNom;
    private JLabel lblInitiales;
    private JLabel lblSalaire;
    private JLabel lblMotPasse1;
    private JLabel lblMotPasse2;
    private JLabel lblMotPasse3;
    private JLabel lblTitre;
    private JLabel lblTache;
    private JLabel lblExisteDeja;
    private JComboBox combTache;
    private JTextField txtNom;
    private JTextField txtInitiales;
    private JTextField txtSalaire;
    private JPasswordField txtMotPasse1;
    private JPasswordField txtMotPasse2;
    private JPasswordField txtMotPasse3;
    private JButton btnModifier;
    private JButton btnAnnuler;
    private JButton btnSupprimer;
    private Travail_Effectue liste_taches;

    public ModifierEmploye(Fenetre fenetre, SearchResult result) {
        this.fenetre = fenetre;
        this.result = result;
        employe = this.result.getEmploye();

        //fenetre.setSize(400, 450);
        fenetre.setTitle(fenetre.getTitle() + " --- Recherche");
        fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComposants();
        buildInterface();

        fenetre.setVisible(true);

        requestFirstFocus();
    }

    private void requestFirstFocus() {
        fenetre.addWindowFocusListener(new WindowAdapter() {

            public void windowGainedFocus(WindowEvent e) {
                txtNom.requestFocusInWindow();
            }
        });
        JFrame trick = new JFrame();
        trick.setVisible(true);
        trick.setVisible(false);
        fenetre.requestFocus();
    }

    private void initComposants() {
        lblTitre = new JLabel("Modifier " + result.getEmploye().getNom());
        lblTitre.setFont(f);

        lblNom = new JLabel("Entrer le nom de l'employé : ");
        lblInitiales = new JLabel("Entrer les initiales de l'employé : ");
        lblSalaire = new JLabel("Entrer le salaire de l'employé ($/h) : ");
        lblMotPasse1 = new JLabel("Entrer l'ancien mot de passe : ");
        lblMotPasse2 = new JLabel("Entrez le nouveau mot de passe : ");
        lblMotPasse3 = new JLabel("Répéter le mot de passe : ");
        lblTache = new JLabel("Sélectionner la tâche habituelle : ");

        lblExisteDeja = new JLabel("Cet employé existe déjà!");
        lblExisteDeja.setForeground(Color.red);
        lblExisteDeja.setVisible(false);

        txtNom = new JTextField();
        txtInitiales = new JTextField();
        txtSalaire = new JTextField();
        txtMotPasse1 = new JPasswordField();
        txtMotPasse2 = new JPasswordField();
        txtMotPasse3 = new JPasswordField();
        combTache = new JComboBox(liste_taches.values());

        txtMotPasse1.addKeyListener(new GererTouches());
        txtMotPasse2.addKeyListener(new GererTouches());
        txtMotPasse3.addKeyListener(new GererTouches());

        txtNom.setText(result.getEmploye().getNom());
        txtInitiales.setText(result.getEmploye().getInitiales());
        txtSalaire.setText(result.getEmploye().getSalaire() + "");
        combTache.setSelectedItem(result.getEmploye().getTache());

        btnModifier = new JButton("Modifier");
        btnAnnuler = new JButton("Annuler");
        btnSupprimer = new JButton("Supprimer");
        btnModifier.addActionListener(new GererEvenements());
        btnAnnuler.addActionListener(new GererEvenements());
        btnSupprimer.addActionListener(new GererEvenements());

        txtNom.addKeyListener(new GererFocus());
        txtInitiales.addKeyListener(new GererFocus());
        txtMotPasse1.addKeyListener(new GererFocus());
        txtMotPasse2.addKeyListener(new GererFocus());
        txtSalaire.addKeyListener(new GererFocus());
        combTache.addKeyListener(new GererFocus());
    }

    private void buildInterface() {
        gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        this.setLayout(gbl);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;
        gbl.setConstraints(lblTitre, gbc);
        this.add(lblTitre);
        makeBorder();
        gbc.anchor = gbc.LINE_END;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(btnSupprimer, gbc);
        //this.add(btnSupprimer);

        makeSeparator();                        //Nom
        makeSeparator();
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.HORIZONTAL;
        gbl.setConstraints(lblNom, gbc);
        this.add(lblNom);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtNom, gbc);
        this.add(txtNom);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();                        //Initiales
        makeBorder();
        gbc.weightx = 0.1;
        gbl.setConstraints(lblInitiales, gbc);
        this.add(lblInitiales);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtInitiales, gbc);
        this.add(txtInitiales);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Mot Passe #1
        gbl.setConstraints(lblMotPasse1, gbc);
        this.add(lblMotPasse1);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtMotPasse1, gbc);
        this.add(txtMotPasse1);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Mot Passe #2
        gbl.setConstraints(lblMotPasse2, gbc);
        this.add(lblMotPasse2);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtMotPasse2, gbc);
        this.add(txtMotPasse2);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Mot Passe #3
        gbl.setConstraints(lblMotPasse3, gbc);
        this.add(lblMotPasse3);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtMotPasse3, gbc);
        this.add(txtMotPasse3);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Salaire
        gbl.setConstraints(lblSalaire, gbc);
        this.add(lblSalaire);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(txtSalaire, gbc);
        this.add(txtSalaire);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;                      //Tache
        gbl.setConstraints(lblTache, gbc);
        this.add(lblTache);
        makeBorder();
        gbc.weightx = 1.0;
        gbl.setConstraints(combTache, gbc);
        this.add(combTache);
        gbc.gridwidth = gbc.REMAINDER;
        makeBorder();

        makeSeparator();                        //Boutons
        makeSeparator();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(btnAnnuler, gbc);
        gbl.setConstraints(btnModifier, gbc);
        this.add(btnAnnuler);
        makeBorder();
        gbl.setConstraints(btnModifier, gbc);
        this.add(btnModifier);
        makeBorder();
        makeSeparator();

        makeBorder();
        makeBorder();
        makeBorder();
        gbc.weightx = 0.1;
        gbc.fill = gbc.NONE;
        gbl.setConstraints(lblExisteDeja, gbc);
        this.add(lblExisteDeja);
        makeBorder();
    }

    private void makeSeparator() {
        JLabel sep = new JLabel(" ");
        gbc.gridwidth = gbc.REMAINDER;
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
        gbc.weighty = 0;
    }

    private void makeBorder() {
        gbc.weightx = 0.1;
        JLabel sep = new JLabel(" ");
        gbl.setConstraints(sep, gbc);
        this.add(sep);
        gbc.gridwidth = gbc.CENTER;
    }

    private boolean verifierDonnees() {
        boolean valide = false;
        btnModifier.setForeground(Color.darkGray);
        lblExisteDeja.setVisible(false);

        if (new String(txtMotPasse1.getPassword()).equals(new String(txtMotPasse2.getPassword())) && txtMotPasse1.getPassword().length != 0) {
            lblMotPasse1.setForeground(Color.darkGray);
            lblMotPasse2.setForeground(Color.darkGray);
            if (!txtNom.getText().isEmpty()) {
                lblNom.setForeground(Color.darkGray);
                if (!txtInitiales.getText().isEmpty()) {
                    lblInitiales.setForeground(Color.darkGray);
                    txtInitiales.setText(txtInitiales.getText().toUpperCase());
                    if (0 < Double.parseDouble(txtSalaire.getText())) {
                        txtSalaire.setForeground(Color.darkGray);
                        if (!alreadyExists()) {
                            valide = true;
                        } else {
                            btnModifier.setForeground(Color.red);
                            lblExisteDeja.setVisible(true);
                        }
                    } else {
                        txtSalaire.setForeground(Color.red);
                        txtSalaire.requestFocusInWindow();
                    }
                } else {
                    lblInitiales.setForeground(Color.red);
                    txtInitiales.requestFocusInWindow();
                }
            } else {
                lblNom.setForeground(Color.red);
                txtNom.requestFocusInWindow();
            }
        } else {
            lblMotPasse1.setForeground(Color.red);
            lblMotPasse2.setForeground(Color.red);
            txtMotPasse1.requestFocusInWindow();
        }

        return valide;
    }

    private boolean alreadyExists() {
        boolean exists = false;
        Employe emTemp;

        for (int i = 0; i < fenetre.arEmploye.size(); i++) {
            emTemp = fenetre.arEmploye.get(i);
            if (emTemp.getNom().equals(txtNom.getText()) || emTemp.getInitiales().equals(txtInitiales.getText())) {
                if (!emTemp.equals(employe)) {
                    exists = true;
                }
            }
        }

        return exists;
    }

    private boolean verifierMotPasse() {
        boolean valide = false;

// if valides ou non
        if (new String(txtMotPasse1.getPassword()).equals(result.getEmploye().getMotPasse())) {
            txtMotPasse1.setForeground(Color.GREEN);
            lblMotPasse1.setForeground(Color.GREEN);
            if (new String(txtMotPasse2.getPassword()).equals(new String(txtMotPasse3.getPassword()))) {
                lblMotPasse2.setForeground(Color.GREEN);
                lblMotPasse3.setForeground(Color.GREEN);
                txtMotPasse2.setForeground(Color.GREEN);
                txtMotPasse3.setForeground(Color.GREEN);
                valide = true;
            } else {
                lblMotPasse2.setForeground(Color.RED);
                lblMotPasse3.setForeground(Color.RED);
                txtMotPasse2.setForeground(Color.RED);
                txtMotPasse3.setForeground(Color.RED);
            }
        } else {
            txtMotPasse1.setForeground(Color.RED);
            lblMotPasse1.setForeground(Color.RED);
        }

// if null
        if (txtMotPasse2.getPassword().length == 0 && txtMotPasse3.getPassword().length == 0) {
            lblMotPasse2.setForeground(Color.BLACK);
            lblMotPasse3.setForeground(Color.BLACK);
            txtMotPasse2.setForeground(Color.BLACK);
            txtMotPasse3.setForeground(Color.BLACK);
            if (txtMotPasse1.getPassword().length == 0) {
                txtMotPasse1.setForeground(Color.BLACK);
                lblMotPasse1.setForeground(Color.BLACK);
            }
        }
        this.revalidate();
        this.repaint();
        return valide;
    }

    class GererEvenements implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btnModifier) {
                if (verifierDonnees()) {
                    Employe temp = result.getEmploye();

                    if (txtMotPasse1.getPassword().length == 0
                            && txtMotPasse2.getPassword().length == 0
                            && txtMotPasse3.getPassword().length == 0) {
                        temp.setInitiales(txtInitiales.getText());
                        temp.setNom(txtNom.getText());
                        temp.setSalaire(Double.parseDouble(txtSalaire.getText()));
                        temp.setTache((Travail_Effectue) combTache.getSelectedItem());
                    } else if (verifierMotPasse()) {
                        temp.setInitiales(txtInitiales.getText());
                        temp.setNom(txtNom.getText());
                        temp.setSalaire(Integer.parseInt(txtSalaire.getText()));
                        temp.setTache((Travail_Effectue) combTache.getSelectedItem());
                        temp.setMotPasse(new String(txtMotPasse2.getPassword()));
                    }


                    try {
                        fenetre.sauvegarderBD();
                        fenetre.AfficherEcranRecherche();
                    } catch (IOException ex) {
                        Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else if (e.getSource() == btnAnnuler) {
                try {
                    fenetre.AfficherEcranRecherche();
                } catch (IOException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (e.getSource() == btnSupprimer) {
                int reponse = JOptionPane.showConfirmDialog(null, "Êtes-vous certain de vouloir supprimer cet employé?", "Confirmation", JOptionPane.YES_NO_OPTION);
                if (reponse == 0) {
                    fenetre.arEmploye.remove(result.getEmploye());
                    try {
                        fenetre.sauvegarderBD();
                        fenetre.AfficherEcranRecherche();
                    } catch (IOException ex) {
                        Logger.getLogger(CreerNouvEmploye.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(CreerNouvClient.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    class GererTouches implements KeyListener {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
            verifierMotPasse();
        }
    }

    class GererFocus implements KeyListener {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
        }

        /**
         * permet de changer le focus d'un champ de texte (JTextField) à un autre grâce à la touche "Entrée".
         * @param e
         * permet d'itentifier la source dans laquelle une touche a étée relâchée.
         */
        public void keyReleased(KeyEvent e) {
            if (e.getSource() == txtNom) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtInitiales.requestFocusInWindow();
                }
            } else if (e.getSource() == txtInitiales) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtMotPasse1.requestFocusInWindow();
                }
            } else if (e.getSource() == txtMotPasse1) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtMotPasse2.requestFocusInWindow();
                }
            } else if (e.getSource() == txtMotPasse2) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    txtSalaire.requestFocusInWindow();
                }
            } else if (e.getSource() == txtSalaire) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    combTache.requestFocusInWindow();
                }
            } else if (e.getSource() == combTache) {
                if (e.getKeyCode() == e.VK_ENTER) {
                    btnModifier.doClick();
                }
            }
        }
    }
}
